package top.infopub.util;


import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;


/**
 * 金钱相关工具类
 * @author Awoke
 * @version 2018年5月13日
 * @see MoneyUtil
 * @since
 */
public final class MoneyUtil {

    /**
     * 数字:100
     */
    public static final int ONE_HUNDRED = 100;

    /**
     * 份额转金额<br>
     * @param share
     * @return
     */
    public static Long shareToAmount(BigDecimal share) {
        return share.multiply(BigDecimal.valueOf(ONE_HUNDRED)).longValue();
    }

    /**
     * 金额转份额<br>
     * @param share
     * @return
     */
    public static BigDecimal amountToShare(Long amount) {
        return BigDecimal.valueOf(amount).divide(BigDecimal.valueOf(ONE_HUNDRED));
    }

    /**
     * 获取银行每日收益
     * @param rate 利率
     * @param amount 金额
     * @return
     */
    public static BigDecimal getBankDayIncome(String rate, long amount) {
        return new BigDecimal(rate).multiply(BigDecimal.valueOf(amount)).divide(
            BigDecimal.valueOf(365), 4);
    }

    /**
     * 元转为分 
     * @param yuan
     * @return 
     * @see
     */
    public static BigDecimal convertYuan2Fen(BigDecimal yuan) {
        if (yuan == null) {
            return new BigDecimal(0);
        }
        return yuan.multiply(new BigDecimal(100)).setScale(0);
    }

    /**
     * 分转为元
     * @param fen
     * @return 
     * @see
     */
    public static BigDecimal convertFen2Yuan(BigDecimal fen) {
        if (fen == null) {
            return new BigDecimal(0);
        }
        return fen.divide(new BigDecimal(100));
    }

    /**
     * 分转为元 
     * @param fen
     * @return
     */
    public static String strFenToStrYuan(String fen) {
        return String.valueOf(BigDecimal.valueOf(Long.parseLong(fen)).divide(
            BigDecimal.valueOf(ONE_HUNDRED)));
    }

    /**
     * 分转元 保留２位小数
     * @param fen
     * @return 
     * @see
     */
    public static String castFentoYuan(String fen) {
        return BigDecimal.valueOf(Double.valueOf(fen)).divide(BigDecimal.valueOf(ONE_HUNDRED)).setScale(
            2, RoundingMode.HALF_UP).toString();
    }

    /**
     * @title 添加会计标识：','
     * @param money 待处理的人民币
     * @return
     */
    public static String getAccountantMoney(BigDecimal money) {
        String disposeMoneyStr = money.setScale(2, RoundingMode.HALF_UP).toString();
        // 小数点处理
        int dotPosition = disposeMoneyStr.indexOf(".");
        String exceptDotMoeny = null;// 小数点之前的字符串
        String dotMeony = null;// 小数点之后的字符串
        if (dotPosition > 0) {
            exceptDotMoeny = disposeMoneyStr.substring(0, dotPosition);
            dotMeony = disposeMoneyStr.substring(dotPosition);
        }
        else {
            exceptDotMoeny = disposeMoneyStr;
        }
        // 负数处理
        int negativePosition = exceptDotMoeny.indexOf("-");
        if (negativePosition == 0) {
            exceptDotMoeny = exceptDotMoeny.substring(1);
        }
        StringBuffer reverseExceptDotMoney = new StringBuffer(exceptDotMoeny);
        reverseExceptDotMoney.reverse();// 字符串倒转
        char[] moneyChar = reverseExceptDotMoney.toString().toCharArray();
        StringBuffer returnMeony = new StringBuffer();// 返回值
        for (int i = 0; i < moneyChar.length; i++ ) {
            if (i != 0 && i % 3 == 0) {
                returnMeony.append(",");// 每隔3位加','
            }
            returnMeony.append(moneyChar[i]);
        }
        returnMeony.reverse();// 字符串倒转
        if (dotPosition > 0) {
            returnMeony.append(dotMeony);
        }
        if (negativePosition == 0) {
            return "-" + returnMeony.toString();
        }
        else {
            return returnMeony.toString();
        }
    }

    /**
     * 转成中文金额
     * @param money
     * @return 
     * @see
     */
    public static String convert2ChineseMoney(BigDecimal money) {
        if (money == null) {
            return null;
        }
        return new MoneyToChinese().numToCNMoney(money.doubleValue());

    }

    /**
     * 转成中文金额
     * @param money
     * @return 
     * @see
     */
    public static String convert2ChineseMoneyDouble(Double money) {
        if (money == null) {
            return null;
        }
        return new MoneyToChinese().numToCNMoney(money.doubleValue());

    }

    /**
     * 转成元转万元且保留两位小数并四舍五入
     * @param value
     * @return 
     * @see
     */
    public static String convert2Tenthousand(String value) {
        BigDecimal bigDecimal = new BigDecimal(value);
        // 转换为万元（除以10000）
        BigDecimal decimal = bigDecimal.divide(new BigDecimal("10000"));
        // 保留两位小数
        DecimalFormat formater = new DecimalFormat("0.000000");
        // 四舍五入
        // formater.setRoundingMode(RoundingMode.HALF_UP);
        // 格式化完成之后得出结果
        String res = formater.format(decimal);
        return subZeroAndDot(res);
    }
    /** 去掉小数点后的0 **/
    private static String subZeroAndDot(String s) {
        if (s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");//去掉多余的0
            s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
        }
        return s;
    }

    public static void main(String[] args) {
        System.out.println(convert2Tenthousand("10019009.10"));
    }
}
